package com.carkeeper.department.controller;


import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.pojo.Department;
import com.carkeeper.department.service.DepartmentService;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询所有部门
     * @return
     */
    @GetMapping()
    public Result findAll(){

        List<Department> list = departmentService.findAll();

        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /**
     * 添加部门
     * @param department
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Department department){

        departmentService.add(department);

        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 根据id修改
     * @param department
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Department department,@PathVariable("id") Integer id){
        department.setId(id);

        departmentService.update(department);

        return new Result(true,StatusCode.OK,"修改成功");
    }

    /*
    根据id删除
     */
    @Delete(value = "/{id}")
    public Result delete(@PathVariable("id") Integer id){

        departmentService.delete(id);

        return new Result(true,StatusCode.OK,"删除成功");
    }
    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Department> pageList = departmentService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

}
